FANCY INPUT The typical INPUT statement looks like this: 10 INPUT "WHAT IS YOUR NAME";N$ It makes the computer ask ``WHAT IS YOUR NAME?'' then wait for you to answer to question. So when you run the program, the conversation looks like this: WHAT IS YOUR NAME? MARIA Notice that the computer automatically adds a question mark at the end of the question, and leaves a blank space after the question mark. Omitting the question mark If you want to omit the question mark and the blank space, replace the semicolon by a comma: 10 INPUT "WHAT IS YOUR NAME",N$ The computer will omit the question mark and the blank space, so the conversation will look like this: WHAT IS YOUR NAMEMARIA Here's a prettier example of how to use the comma: 10 INPUT "PLEASE TYPE YOUR NAME...",N$ The conversation will look like this: PLEASE TYPE YOUR NAME...MARIA Here's an even prettier example: 10 INPUT "TO BECOME A MOVIE STAR, TYPE YOUR NAME NEXT TO THE STARS***";N$ The conversation will look like this: TO BECOME A MOVIE STAR, TYPE YOUR NAME NEXT TO THE STARS***MARIA Omitting the prompt The typical INPUT statement contains a question, such as ``WHAT IS YOUR NAME''. The question is called the prompt. If you wish, you can omit the prompt, like this: 10 INPUT N$ That line doesn't include a question, but the computer still prints a question mark followed by a blank space, so the conversation looks like this: ? MARIA To make that example more practical, add a PRINT statement above line 10, like this: 9 PRINT "PLEASE TYPE YOUR NAME AFTER THE QUESTION MARK" 10 INPUT N$ That makes the conversation look like this: PLEASE TYPE YOUR NAME AFTER THE QUESTION MARK ? MARIA Adjacent printing Here's a simple program: 10 INPUT "WHAT IS YOUR NAME";N$ 20 PRINT "!!!WHAT A WONDERFUL NAME!!!" It produces this conversation: WHAT IS YOUR NAME? MARIA !!!WHAT A WONDERFUL NAME!!! To have more fun, insert a semicolon immediately after the word INPUT, like this: 10 INPUT;"WHAT IS YOUR NAME";N$ 20 PRINT "!!!WHAT A WONDERFUL NAME!!!" The conversation will begin normally: WHAT IS YOUR NAME? MARIA But when you press the ENTER key after MARY, the extra semicolon makes the computer print line 20 next to MARIA, like this: WHAT IS YOUR NAME? MARIA!!!WHAT A WONDERFUL NAME!!! To surprise your friends, run this program: 10 INPUT;"WHAT IS YOUR NAME";N$ 20 PRINT N$;N$;N$ The program begins by asking: WHAT IS YOUR NAME? Suppose the person says MARIA, like this: WHAT IS YOUR NAME? MARIA When the person presses the ENTER key after MARIA, line 20 automatically prints MARIA three more times afterwards, like this: WHAT IS YOUR NAME? MARIAMARIAMARIAMARIA This program asks for your first name, then your last name: 10 INPUT;"WHAT IS YOUR FIRST NAME";F$ 20 INPUT " WHAT IS YOUR LAST NAME";L$ Line 10 makes the conversation begin like this: WHAT IS YOUR FIRST NAME? MARIA When you press the ENTER key after MARIA, the extra semicolon in line 10 makes line 20 appear on the same line, like this: WHAT IS YOUR FIRST NAME? MARIA WHAT IS YOUR LAST NAME? If you answer WONG, the whole conversation looks like this: WHAT IS YOUR FIRST NAME? MARIA WHAT IS YOUR LAST NAME? YEE Multiple input This program asks for your name, age, and weight: 10 INPUT "NAME, AGE, WEIGHT";N$,A,W When you run the program, the computer asks: NAME, AGE, WEIGHT? The computer waits for you to type your name, age, and weight. When you type them, put commas between them, like this: NAME, AGE, WEIGHT? JOHN,25,148 If your name is ``JOHN SMITH, JR.'', and you want to input all that instead of just JOHN, you must put quotation marks around your name: NAME, AGE, WEIGHT? "JOHN SMITH, JR.",25,148 Here's the rule: you must put quotation marks around any INPUT string that contains a comma. LINE INPUT If you say ___ 10 LINE INPUT "PLEASE TYPE YOUR NAME...";N$ the computer will say: PLEASE TYPE YOUR NAME... Then the computer will wait for you to type your name. You do not have to put quotation marks around your name, even if your name contains a comma. LINE INPUT means: the entire line that the person inputs will become the string, even if the line contains a comma. Notice that the LINE INPUT statement does not make the computer automatically print a question mark. And notice that the variable must be a string (such as N$), not a number. INPUT$ This program reveals private information about MARY: 10 PRINT "MARY SECRETLY WISHES TO KISS A COW" Here's how to protect that program, so only people who know the ``secret password'' can run it. . . . First, invent a secret password. Let's make it be ``TUNA''. Here's the program: 5 INPUT "WHAT'S THE SECRET PASSWORD";P$ 10 IF P$="TUNA" THEN PRINT "MARY SECRETLY WISHES TO KISS A C OW" ELSE PRINT "YOU ARE AN UNAUTHORIZED USER" Line 5 asks the person to type the secret password. Whatever the person types is called P$. If the person types TUNA, line 10 makes the computer say: MARY SECRETLY WISHES TO KISS A COW But if the person does not type TUNA, the computer says ``YOU ARE AN UNAUTHORIZED USER'' and refuses to reveal Mary's secret desire. This program's better: 5 PRINT "PLEASE TYPE THE SECRET PASSWORD" 6 P$=INPUT$(4) 10 IF P$="TUNA" THEN PRINT "MARY SECRETLY WISHES TO KISS A C OW" ELSE PRINT "YOU ARE AN UNAUTHORIZED USER" Line 5 makes the computer say: PLEASE TYPE THE SECRET PASSWORD Line 6 waits for the person to input 4 characters. The characters that the person inputs will become P$. For example, suppose the person types T, then U, then N, then A; then P$ will become TUNA and the computer will reveal Mary's secret. While the person inputs the 4 characters, they won't appear on the screen; they'll be invisible. That's to prevent other people in the room from peeking at the screen and noticing the password. After typing the 4 characters, the person does not have to press the ENTER key. As soon as the person types the 4th character, the computer makes P$ be the 4 characters that the person typed. Broken computer This devilish program makes your computer pretend to be broken, so that whenever you press the W key your screen shows an F instead: 10 CLS 20 A$=INPUT$(1) 30 IF A$="W" THEN PRINT "F"; ELSE PRINT A$; 40 GO TO 20 Line 10 clears the screen (makes the screen go blank), so that your friends don't see the program. Line 20 waits for you to type 1 character. Line 30 says: if the character you typed was W, print an F on the screen instead; otherwise, print the character you typed. Line 40 makes the routine repeat, forever. For example, if you try to type ``THE WEATHER IS WONDERFUL'', you'll see this on the screen instead: THE FEATHER IS FONDERFUL For an even wilder time, tell the computer to change each ``E'' to ``OOGA'', by changing line 30 to this: 30 IF A$="E" THEN PRINT "OOGA"; ELSE PRINT A$; Then if you try to type ``WE ARE HERE'', you'll see this on the screen instead: WOOGA AROOGA HOOGAROOGA Abridged literature This program gives you a choice of literature: 10 PRINT "WELCOME TO THE WORLD'S GREAT LITERATURE, ABRIDGED" 20 PRINT "WHICH KIND OF LITERATURE WOULD YOU LIKE?" 30 PRINT "N: NOVEL" 40 PRINT "P: POEM" 50 PRINT "PLEASE PRESS N OR P" 60 A$=INPUT$(1) 70 IF A$="N" THEN GO TO 100 80 IF A$="P" THEN GO TO 200 90 GO TO 50 100 PRINT "HE: I LOVE YOU" 110 PRINT "SHE: I'M PREGNANT" 120 PRINT "HE: LET'S GET MARRIED" 130 PRINT "SHE: LET'S GET DIVORCED" 140 PRINT "HE: LET'S GET BACK TOGETHER" 150 PRINT "SHE: TOO BAD YOU DIED IN THE WAR, BUT I'LL NEVER FORGET YOU!" 160 END 200 PRINT "NOSES" 210 PRINT "BLOWSES" Lines 10-50 make the computer print: WELCOME TO THE WORLD'S GREAT LITERATURE, ABRIDGED WHICH KIND OF LITERATURE WOULD YOU LIKE? N: NOVEL P: POEM PLEASE PRESS N OR P Line 60 makes the computer wait for you to press a key. You do not have to press the ENTER key afterwards. If you press the N key, line 70 sends the computer to line 100, which prints an abridged novel. If you press the P key instead, line 80 sends the computer to line 200, which prints an abridged poem. If you press neither N nor P, the computer arrives at line 90, which sends the computer back to line 50, which reminds you to press N or P. JOYSTICK VERSUS MOUSE Instead of using the keyboard, you can input by using a joystick or mouse. Joystick A joystick is a stick that sticks up from a box. You can wiggle the stick in all four directions (left, right, forward, and back) and diagonally. In line 100 of your program, if you want the computer to look at the joystick, say: 100 X=STICK(0): Y=STICK(1) That line makes X become a number that indicates how far the joystick is being pulled to the right, and Y become a number that indicates how far the joystick is being pushed forward. To make the computer print X and Y on your screen, say: 110 PRINT X,Y To make the computer look at the joystick again and again, repeatedly, create a loop, by adding this line: 120 GO TO 100 Then as you wiggle the joystick, you can watch the numbers on the screen change. Besides printing the numbers X and Y on the screen, you can use the numbers X and Y in graphics statements (such as PLOT, LINE, and CIRCLE), so that the joystick controls the location of graphics shapes on the screen. You can also use the X and Y numbers in the SOUND statement, so that the joystick controls the pitch of musical sounds: moving the joystick will change the pitch. Mouse A mouse is a small box about the size of a pack of cigarettes. You can slide the mouse across your desk in all four directions (left, right, forward, and back) and diagonally. Some versions of BASIC let your program contain commands to handle a mouse. Those commands are called mouse commands. (GWBASIC and QBASIC do not let programs contain mouse commands. If you're using GWBASIC or QBASIC, skip ahead to the next page.) If your computer's BASIC understands mouse commands, and you want the computer to look at the mouse in line 100 of your program, say this: 100 M=MOUSE(0): X=MOUSE(1): Y=MOUSE(2) In that line, the ``M=MOUSE(0)'' makes the computer look at the mouse. The rest of the line makes X become a number that indicates how far the mouse was moved to the right, and Y become a number that indicates how far the mouse was moved forward. To make the computer print X and Y on your screen, say: 110 PRINT X,Y To make the computer look at the mouse again and again repeatedly, create a loop by adding this line: 120 GO TO 100 Then as you move the mouse across your desk, you can watch the numbers on the screen change. SWAP Modern computers understand the word SWAP: 10 A=4: B=9 20 SWAP A,B 30 PRINT A;B Line 10 says A is 4 and B is 9. Line 20 swaps A with B, so that A becomes 9, and B becomes 4. Line 30 prints: 9 4 If your computer is old-fashioned and doesn't understand the word SWAP, replace line 20 by this: 20 S=A: A=B: B=S That example swapped numbers. You can also swap strings: 10 A$="HORSE": B$="CART" 20 SWAP A$,B$ 30 PRINT A$;" ";B$ Line 10 says A$ is ``HORSE'' and B$ is ``CART''. Line 20 swaps A$ with B$, so that A$ becomes ``CART'', and B$ becomes ``HORSE''. Line 30 puts the CART before the HORSE: CART HORSE If your computer doesn't understand SWAP, replace line 20 by this: 20 S$=A$: A$=B$: B$=S$ Don't forget the dollar signs! Shuffling Here are some cards: Queen of Hearts Jack of Diamonds Ace of Spades Joker King of Clubs Let's shuffle them, to put them into a random order. To begin, put the list of cards into a DATA statement: 10 DATA QUEEN OF HEARTS,JACK OF DIAMONDS,ACE OF SPADES,JOKER,KING OF CLUBS We have 5 cards: 20 N=5 Let the Queen of Hearts be called X$(1), the Jack of Diamonds be called X$(2), the Ace of Spades be called X$(3), the Joker be called X$(4),and the King of Clubs be called X$(5): 30 DIM X$(N) 40 FOR I = 1 TO N: READ X$(I): NEXT Shuffle the cards, by using the following strategy. . . . Swap the card N with a random card before it (or with itself); then swap card N-1 with a random card before it (or with itself); then swap card N-2 with a random card before it (or with itself); etc. Keep doing that, until you finally reach card 2, which you swap with a random card before it (or with itself). Here's the code: 50 RANDOMIZE 60 FOR I = N TO 2 STEP -1: SWAP X$(I),X$(RND(I)): NEXT If your computer doesn't understand SWAP, replace ``SWAP X$(I),X$(RND(I))'' by this: R=RND(I): S$=X$(I): X$(I)=X$(R): X$(R)=S$ If your computer doesn't understand the word RANDOMIZE, or doesn't understand that RND(I) is a random number from 1 to I, you must make further changes. Finally, print the shuffled deck: 70 FOR I = 1 TO N: PRINT X$(I): NEXT The computer will print something like this: KING OF CLUBS JOKER JACK OF DIAMONDS QUEEN OF HEARTS ACE OF SPADES To shuffle a larger deck, change just lines 10 and 20. Sorting Putting words in alphabetical order ___ or putting numbers in numerical order ___ is called sorting. Short Three-Line Sort Here are a dozen names: Sue, Ann, Joe, Alice, Ted, Jill, Fred, Al, Sam, Pat, Sally, Moe. Let's make the computer alphabetize them (sort them). To begin, put the list of names into a DATA statement: 10 DATA SUE,ANN,JOE,ALICE,TED,JILL,FRED,AL,SAM,PAT,SALLY,MOE We have 12 names: 20 N=12 Let Sue be called X$(1), Ann be called X$(2), Joe be called X$(3), etc. Here's how: 30 DIM X$(N) 40 FOR I = 1 TO N: READ X$(I): NEXT Alphabetize the names, by using the following strategy. . . . Compare the first name against the second; if they're not in alphabetical order, swap them. Compare the second name against the third; if they're not in alphabetical order, swap them. Compare the third name against the fourth; if they're not in alphabetical order, swap them. Continue that process, until you finally compare the last two names. But each time you swap, you must start the whole process over again, to make sure the preceding names are still in alphabetical order. Here's the code: 50 FOR I = 1 TO N-1 60 IF X$(I)>X$(I+1) THEN SWAP X$(I),X$(I+1): GO TO 50 70 NEXT Finally, print the alphabetized list: 80 FOR I = 1 TO N: PRINT X$(I): NEXT The computer will print: AL ALICE ANN FRED JILL JOE MOE PAT SALLY SAM SUE TED In that program, the sorting occurs in lines 50-70. Those three short lines are called the Short Three-Line Sort. Those three lines form a loop. The part of the loop that the computer encounters the most often is the phrase ``IF X$(I)>X$(I+1)''. In fact, if you tell the computer to alphabetize a list of names that's long (several hundred names), the computer spends the majority of its time repeatedly handling ``IF X$(I)>X$(I+1)''. How long will the computer take to handle a long list of names? The length of time depends mainly on how often the computer encounters the phrase ``IF X$(I)>X$(I+1)''. If the list contains N names, the number of times that the computer encounters the phrase ``IF X$(I)>X$(I+1)'' is approximately N3/8. Here are some examples: N (number of names)N3/8 (approximate number of encounters) 10 125 12 216 20 1,000 40 8,000 100 125,000 1,000 125,000,000 10,000 125,000,000,000 For example, the chart says that a list of 12 names requires approximately 216 encounters. The 216 is just an approximation: the exact number of encounters depends on which list of names you're trying to alphabetize. If the list is nearly in alphabetical order already, the number of encounters will be less than 216; if the list is in reverse alphabetical order, the number of encounters will be more than 216; but if the list is typical (not yet in any particular order), the number of encounters will be about 216. For the list that we tried (Sue, Ann, Joe, Alice, Ted, Jill, Fred, Al, Sam, Pat, Sally, Moe), the exact number of encounters happens to be 189, which is close to 216. How long will your computer take to finish sorting? The length of time depends on which computer you have, how many names are in the list, and how long each name is. A typical microcomputer (handling a list of typical names) requires .01 seconds per encounter. Multiplying the number of encounters by .01 seconds, you get: Number of namesEncounters (N3/8) Time 10 125 1.25 secs 12 216 2.16 secs 20 1,000 10 secs 40 8,000 80 secs = 1.3 minutes 100 125,000 1,250 secs = 20.8 minutes 1,000 125,000,000 1,250,000 secs = 14.4 days 10,000 125,000,000,0001,250,000,000 secs = 39.6 years Moral: never let a 70-year-old programmer alphabetize a list of 10,000 names by using the Short Three-Line Sort ___ because when the computer finishes running the program (39.6 years later), the programmer will probably be dead! Fancy Three-Line Sort In the Short Three-Line Sort, the end of line 60 says ``GO TO 50''. For an interesting experiment, replace the ``GO TO 50'' by ``IF I>1 THEN I=I-1: GO TO 60'', so that line 60 looks like this: 60 IF X$(I)>X$(I+1) THEN SWAP X$(I),X$(I+1): IF I>1 THEN I=I-1: GO TO 60 Even though that new, fancy version of line 60 is longer to type than the original, the computer handles it more quickly, because it tells the computer to GO TO line 60 instead of forcing the computer to go all the way back to line 50. The fancy version is called the Fancy Three-Line Sort. If you feed the computer the Fancy Three-Line Sort (instead of the Short Three-Line Sort), the computer encounters the ``IF X$(I)>X$(I+1)'' less often: just N2/2 times (instead of N3/8). Number of namesEncounters (N2/2) Time 10 50 .50 secs 12 72 .72 secs 20 200 2 secs 40 800 8 secs 100 5,000 50 secs 1,000 500,000 5,000 secs = 1.4 hours 10,000 50,000,000 500,000 secs = 5.8 days Now you can hire the 70-year old programmer: to alphabetize 10,000 names, he'll need just 5.8 days instead of 39.6 years. By changing just the end of line 60, we saved almost 40 years of computer time ___ and the programmer's job! Super-Fancy Three-Line Sort To reduce the time even further, use the Super-Fancy Three-Line Sort instead, which is: 50 FOR I = 1 TO N-1 60 FOR J = I TO 1 STEP -1: IF X$(J)>X$(J+1) THEN SWAP X$(J),X$(J+1): NEXT 70 NEXT I When typing the Super-Fancy Three-Line Sort, make sure you type line 60 correctly: you must put the IF on the same line as the NEXT. In line 70, make sure you put the ``I'' after NEXT; otherwise, the computer might think you mean NEXT J. For the Super-Fancy Three-Line Sort, the number of times the computer encounters the phrase ``IF X$(J)>X$(J+1)'' is just N2/4: Number of namesEncounters (N2/4) Time 10 25 .25 secs 12 36 .36 secs 20 100 1 secs 40 400 4 secs 100 2,500 25 secs 1,000 250,000 2,500 secs = 41.7 minutes 10,000 25,000,000 250,000 secs = 2.9 days Six-Line Sort The Six-Line Sort reduces the time even further. To construct the Six-Line Sort, begin with the Super-Fancy Three-Line Sort, but change each +1 to +D, and change each -1 to -D, so that you get: 50 FOR I = 1 TO N-D 60 FOR J = I TO 1 STEP -D: IF X$(J)>X$(J+D) THEN SWAP X$(J),X$(J+D): NEXT 70 NEXT I D begins by being N ___ 42 D=N but then decreases, so that it becomes a fifth of its original value: 44 D=INT(D/5)+1 After performing lines 50-70 for that D, check whether D is down to 1 yet; if it isn't, repeat the process: 75 IF D>1 THEN GO TO 44 So altogether, the complete Six-Line Sort looks like this: 42 D=N 44 D=INT(D/5)+1 50 FOR I = 1 TO N-D 60 FOR J = I TO 1 STEP -D: IF X$(J)>X$(J+D) THEN SWAP X$(J),X$(J+D): NEXT 70 NEXT I 75 IF D>1 THEN GO TO 44 For the Six-Line Sort, the number of times the computer encounters the phrase ``IF X$(J)>X$(J+D)'' is just 1.5*(N-1)^(4/3): Number of namesEncounters Time 10 28 .28 secs 12 37 .37 secs 20 76 .76 secs 40 198 1.98 secs 100 687 6.87 secs 1,000 14,980 149.80 secs = 2.5 minutes 10,000 323,122 3,231.22 secs = 53.9 minutes Notice that the Six-Line Sort handles 10,000 names in 53.9 minutes. That's much faster than the Super-Fancy Three-Line Sort, which takes 2.9 days. But to handle just 10 names, the Six-Line Sort does not run faster than the Super-Fancy Three-Line Sort. So use the Six-Line Sort just for handling long lists of names. To make the Six-Line Sort even faster, add this line: 1 DEFINT A-Z That tells the computer that none of the variables stands for a decimal. By saying DEFINT A-Z, you enable the computer to run the program 20% faster, so that the timing looks like this: Number of names Time 10 .2 secs 12 .3 secs 20 .6 secs 40 1.6 secs 100 5.5 secs 1,000 120 secs = 2 minutes 10,000 2,580 secs = 43 minutes We've sure come a long way! Our first attempt (the Short Three-Line Sort) took 39.6 years to alphabetize 10,000 names; our newest attempt (the Six-Line Sort with DEFINT) takes just 43 minutes. If you try running those sorting methods on your computer, you'll find the timings are slightly different, since the exact timings depend on which computer you have, the length of each name, and how badly the names are out of order. Famous sorts Although I ``invented'' all those sorting methods, most of them are just slight improvements on methods that were developed by others. For example, the Super-Fancy Three-Line Sort is a slight improvement on the Shuttle Sort, which was invented by Shaw & Trimble in 1983. The Six-Line Sort is a slight improvement on the Shell Sort, which was invented by Donald Shell in 1959 and further developed by Hibbard & Boothroyd in 1963, Peterson & Russell in 1971, and Knuth in 1973. Phone directory Suppose you want to alphabetize this phone directory: Name Phone number Mary Smith277-8139 John Doe513-9134 Russ Walter666-2666 Information555-1212 Just use one of the alphabetizing programs I showed you! Type the DATA like this: 10 DATA SMITH MARY 277-8139,DOE JOHN 513-9134 20 DATA WALTER RUSS 666-2666,INFORMATION 555-1212 The computer will print: DOE JOHN 513-9134 INFORMATION 555-1212 SMITH MARY 277-8139 WALTER RUSS 666-2666 Sorting numbers Suppose you want to put a list of numbers into increasing order. For example, if the numbers are 51, 4.257, -814, 62, and .2, let's make the computer print: -814 .2 4.257 51 62 To do that, just use one of the alphabetizing programs I showed you ___ but in the DATA statement, put the numbers instead of strings; and remove the dollar signs (say X instead of X$). To put a list of numbers into decreasing order, begin by writing a program that puts them into increasing order, and then change line 60 from ``>X'' to ``"X" THEN GOSUB 6000 4030 RETURN Subroutine 5000: change the info Agree to change the info.5000 PRINT "OKAY. I'VE ERASED THAT INFORMATION ABOUT "T$"." Request new info.5010 PRINT: PRINT "TYPE WHAT YOU WANT ME TO KNOW ABOUT "T$: PRINT "(IF YOU WANT ME TO FORGET "T$", TYPE THE LETTER X)": INPUT D$ Change the info as requested.5020 IF D$="X" THEN GOSUB 7000 ELSE D$(I)=D$ 5030 RETURN Subroutine 6000: insert the topic into the database Increase the number of topics.6000 N=N+1 Append new topic & its data.6010 T$(N)=T$: D$(N)=D$ 6020 RETURN Subroutine 7000: delete the topic from the database Replace that topic by topic #N.7000 T$(I)=T$(N): D$(I)=D$(N) Decrease the number of topics.7010 N=N-1 7020 RETURN If your computer doesn't understand DEFINT, omit line 10. If your computer doesn't understand LINE INPUT, omit the word LINE from lines 40 and 4010. The program stores the topics in chronological order: if you begin by feeding it information about SUE and then information about CAROL, it will let T$(1) be SUE and let T$(2) be CAROL. Alphabetical database Instead of chronological order, you might prefer alphabetical order. For example, suppose you feed the computer information about SUE then CAROL then ZELDA then ALICE then JANE. Here's what the computer's memory would look like, in each kind of order: Chronological orderAlphabetical order SUE ALICE CAROL CAROL ZELDA JANE ALICE SUE JANE ZELDA Which is better: chronological order or alphabetical order? Chronological order lets you quickly add a new name (just add it at the end of the list), but finding a name in the list is slow (since the list looks disorganized). Alphabetical order lets you find a name faster (since the list is alphabetized), but adding a new name to the alphabetized list is slow (since the only way to insert the new name is to make room for it by shoving other names out of the way). So which is better? Chronological order is the simplest to program and the fastest for INSERTING. Alphabetical order is the fastest for FINDING information. If you want to store the names in alphabetical order instead of chronological order, just change subroutines 2000, 6000, and 7000 to these: Subroutine 2000A: search through the database, to find the topic T$ Create L and H. 2000 L=0: H=N+1 I is the average of L and H.2010 I=INT((L+H+1)/2) If I=H, do 4000.2020 IF I=H THEN GOSUB 4000: RETURN If the topic is found, do 3000.2030 IF T$=T$(I) THEN GOSUB 3000: RETURN Not found yet. Change H or L2040 IF T$0 THEN T$=T$(B): I=I1: GOSUB 8000: P(I1,J)=B If the vanishing topic isn't topic N, move topic N to the gap left by the vanishing topic. 7030 IF A0 THEN GO TO 8000 8040 RETURN Disk-based database All those database programs (chronological, alphabetical, and tree) put data into the RAM. When you turn off the power, the RAM forgets all the data! This superior program puts the database onto a disk instead of into RAM: The main routine D Prepare the variables.10 DEFINT A-Z: DIM PF$(2): Z$=MKI$(0) Open the file and its fields.20 OPEN "INFO" AS 1 LEN=128: FIELD 1, 44 AS TF$, 80 AS DF$, 2 AS PF$(1), 2 AS PF$(2) Topics #1 and #2 are headers. Topic #2 should be "N !", and topic #1's left pointer should be N. 30 IF LOF(1)=0 THEN N=2: LSET TF$="N !": LSET PF$(1)=Z$: LSET PF$(2)=Z$: PUT 1,2 ELSE GET 1,1: N=CV I(PF$(1)) Ask the human for a topic.40 PRINT: PRINT "WHAT TOPIC INTERESTS YOU?": PRINT "(IF YOU'RE NOT SURE, TYPE A QUESTION MARK)": PR INT "(IF YOU WANT TO END, TYPE THE LETTER X)": LINE INPUT T$ If the human wishes, do 1000.50 IF T$="?" THEN GOSUB 1000: GO TO 40 If the human wishes, end.60 IF T$="X" THEN LSET PF$(1)=MKI$(N): PUT 1,1: CLOSE: END Make TS$ resemble T$ but be 44 characters long (by adding blank spaces at the end). 70 LSET TF$=T$: TS$=TF$ Find the topic. 80 GOSUB 2000 Go to another topic.90 GO TO 40 Subroutine 1000D: tell the human what topics are in the file If just headers, say so.1000 IF N=2 THEN PRINT "I DON'T KNOW ANY TOPICS YET.": PRINT "MY MIND IS STILL BLANK.": PRINT "PLEA SE TEACH ME A NEW TOPIC.": RETURN If file has topics, list them.1010 PRINT "I KNOW ABOUT THESE TOPICS:": FOR I = 3 TO N: GET 1,I: PRINT TF$: NEXT: PRINT "PICK ONE OF THOSE TOPICS, OR TEACH ME A NEW ONE." 1020 RETURN Subroutine 2000D: search through the file, to find the topic TS$ Starting at 2, hunt for TS$.2000 I=2: GOSUB 8000 If absent, do 4000, else 3000.2010 IF I=0 THEN GOSUB 4000 ELSE GOSUB 3000 2020 RETURN Subroutine 3000D: the topic's in the file Tell the human about the topic.3000 PRINT "HERE'S WHAT I KNOW ABOUT "T$":": PRINT DF$ Ask whether to change the info.3010 INPUT "DO YOU WANT TO CHANGE THAT INFORMATION";A$ If the human says yes, do 5000.3020 IF A$="YES" OR A$="Y" THEN GOSUB 5000 3030 RETURN Subroutine 4000D: the topic's not in the file Say topic is not in the file.4000 PRINT "I DON'T KNOW ANYTHING ABOUT "T$"." Request info about the topic.4010 PRINT: PRINT "TELL ME ABOUT "T$: PRINT "(IF YOU DON'T WANT TO TELL ME, TYPE THE LETTER X)": LI NE INPUT D$ If human wants, insert topic.4020 IF D$<>"X" THEN GOSUB 6000 4030 RETURN Subroutine 5000D: change the info Agree to change the info.5000 PRINT "OKAY. I'VE ERASED THAT INFORMATION ABOUT "T$"." Request new info.5010 PRINT: PRINT "TYPE WHAT YOU WANT ME TO KNOW ABOUT "T$: PRINT "(IF YOU WANT ME TO FORGET "T$", TYPE THE LETTER X)": LINE INPUT D$ Change the info as requested.5020 IF D$="X" THEN GOSUB 7000 ELSE LSET DF$=D$: PUT 1,I 5030 RETURN Subroutine 6000D: insert the topic into the file Increase the number of topics.6000 N=N+1 Let topic I1 point to topic N.6010 LSET PF$(J)=MKI$(N): PUT 1,I1 Append the new topic, at N.6020 LSET TF$=TS$: LSET DF$=D$: LSET PF$(1)=Z$: LSET PF$(2)=Z$: PUT 1,N 6030 RETURN Subroutine 7000D: delete the topic from the file A is the number of the vanishing topic. P1 and P2 are the topic's pointers. 7000 A=I: P1=CVI(PF$(1)): P2=CVI(PF$(2)) Make topic I1 (which was pointing to the vanishing topic) point to the vanishing topic's left child instead. 7010 GET 1,I1: LSET PF$(J)=MKI$(P1): PUT 1,I1 If the vanishing topic has a right child, make something point to that child. 7020 IF P2>0 THEN GET 1,P2: TS$=TF$: I=I1: GOSUB 8000: LSET PF$(J)=MKI$(P2): PUT 1,I1 If the vanishing topic isn't topic N, move topic N to the gap left by the vanishing topic. 7030 IF A0 THEN GO TO 8000 8050 RETURN